package com.xiongtian.springframework.aop.framework;

import org.aopalliance.intercept.MethodInvocation;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;

/**
 * @Author: xiongtian
 * @CreateTime: 2025-04-17  12:11
 * @Version: 1.0
 * @Description: <p>Invokes the target object using reflection. Subclasses can override the
 * #invokeJoinpoint() method to change this behavior, so this is also
 * a useful base class for more specialized MethodInvocation implementations.
 * <p>
 */

public class ReflectiveMethodInvocation implements MethodInvocation {

    /**
     * 目标对象
     */
    protected final Object target;

    /**
     * 被代理的方法
     */
    protected final Method method;

    /**
     * 方法参数
     */
    protected final Object[] arguments;

    public ReflectiveMethodInvocation(Object target, Method method, Object[] arguments) {
        this.target = target;
        this.method = method;
        this.arguments = arguments;
    }


    @Override
    public Method getMethod() {
        return method;
    }

    @Override
    public Object[] getArguments() {
        return arguments;
    }

    /**
     * 执行目标方法
     *
     * @return 目标方法的返回值
     * @throws Throwable 目标方法抛出的异常
     */
    @Override
    public Object proceed() throws Throwable {
        return method.invoke(target, arguments);
    }

    /**
     * 获取目标对象
     *
     * @return 目标对象
     */
    @Override
    public Object getThis() {
        return target;
    }

    /**
     * 获取静态部分
     *
     * @return 静态部分
     */
    @Override
    public AccessibleObject getStaticPart() {
        return method;
    }
}
